package com.example.logistics.config;

import com.example.logistics.entity.*;
import com.example.logistics.repository.*;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.OffsetDateTime;
import java.util.List;

@Configuration
@RequiredArgsConstructor
public class DataLoader {

    @Bean
    CommandLineRunner initData(WarehouseRepository warehouses,
                               InventoryItemRepository items,
                               CarrierRepository carriers,
                               CustomerOrderRepository orders,
                               ShipmentRepository shipments) {
        return args -> {
            if (warehouses.count() == 0) {
                Warehouse w1 = warehouses.save(Warehouse.builder().name("上海一号仓").location("上海市浦东新区").capacity(10000).build());
                Warehouse w2 = warehouses.save(Warehouse.builder().name("深圳二号仓").location("深圳市南山区").capacity(8000).build());

                items.saveAll(List.of(
                        InventoryItem.builder().sku("SKU-1001").name("手机").quantity(500).warehouse(w1).build(),
                        InventoryItem.builder().sku("SKU-1002").name("耳机").quantity(1200).warehouse(w1).build(),
                        InventoryItem.builder().sku("SKU-2001").name("电脑").quantity(300).warehouse(w2).build()
                ));

                Carrier c1 = carriers.save(Carrier.builder().name("顺丰").contactEmail("sf@example.com").contactPhone("400-811-1111").build());
                Carrier c2 = carriers.save(Carrier.builder().name("中通").contactEmail("zt@example.com").contactPhone("400-822-2222").build());

                CustomerOrder o1 = CustomerOrder.builder()
                        .orderNumber("ORD-0001")
                        .status(OrderStatus.NEW)
                        .createdAt(OffsetDateTime.now())
                        .warehouse(w1)
                        .build();
                o1.getItems().add(OrderItem.builder().order(o1).sku("SKU-1001").name("手机").quantity(2).build());
                o1.getItems().add(OrderItem.builder().order(o1).sku("SKU-1002").name("耳机").quantity(3).build());
                orders.save(o1);

                Shipment s1 = Shipment.builder()
                        .trackingNumber("TRK-0001")
                        .status(ShipmentStatus.CREATED)
                        .order(o1)
                        .carrier(c1)
                        .shippedAt(OffsetDateTime.now())
                        .build();
                shipments.save(s1);
            }
        };
    }
}


